# -*- coding: utf-8 -*-
"""
"Playing to the Gallery: Emotive Rhetoric in Parliaments"
Moritz Osnabruegge, Sara B. Hobolt, Toni Rodon
--
This script produces Figure 1
"""


import os
import numpy as np
import pandas as pd
import matplotlib
import wordcloud
from wordcloud import WordCloud



#Specify the path here
os.chdir("")


emotive = pd.read_csv("emotive_cloud.csv", encoding="UTF-8")
neutral = pd.read_csv("neutral_cloud.csv", encoding="UTF-8")
neutral["score_rescaled"] = neutral["score_rescaled"].abs()


#Make dictionary
emotive_dict = pd.Series(emotive.score_rescaled.values,index=emotive.word).to_dict()
neutral_dict = pd.Series(neutral.score_rescaled.values,index=neutral.word).to_dict()


#Define color function
np.random.seed(seed=10)

def colorfunc(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None): 
    color = np.random.randint(maincol-10, maincol+10) 
    return "hsl(%d, %d%%, %d%%)" % (color, np.random.randint(65,75)+font_size/7, np.random.randint(35,45)-font_size/10)   
   

#Make word cloud with emotive words
maincol = 350

emotive_wordcloud = WordCloud(background_color="white", max_font_size=100, 
                    color_func=colorfunc, height=600, width=1000, random_state=180).generate_from_frequencies(emotive_dict)

emotive_wordcloud.to_file("figure_1a.png")


#Make word cloud with neutral words
maincol = 150

neutral_wordcloud = WordCloud(background_color="white", max_font_size=100, 
                    color_func=colorfunc, height=600, width=1000, random_state=180).generate_from_frequencies(neutral_dict)

neutral_wordcloud.to_file("figure_1b.png") 
